<?php

/**
 * This is the model class for table "tbl_news".
 *
 * The followings are the available columns in table 'tbl_news':
 * @property integer $news_id
 * @property integer $user_id
 * @property integer $newstype_id
 * @property integer $priority
 * @property integer $views
 * @property string $published_date
 * @property string $title
 * @property string $content
 * @property integer $status
 * @property string $url
 * The followings are the available model relations:
 * @property User $user
 */
class News extends CActiveRecord
{

    const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;

    const TYPE_GENERAL = 1;
    const TYPE_TOPSCROLL = 2;
    const TYPE_IMAGECONTENT = 3;
    const TYPE_ANNOUNCEMENT = 4;
    const TYPE_HEADLINES = 5;
    const TYPE_HEADIMAGE = 6;

    const CONTENT_LENGTH = 200;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_news';
    }
    /**
     * get the dataprovider by newsType
     * @author : nonglei
     * @date   : 12/05/2014
     */
    public static function getNewsByNewsType($newsType)
    {
        $dataProvider=new CActiveDataProvider('News',array(
            'criteria'=>array(
            'condition'=>'newstype_id = '.$newsType.' and status='.News::STATUS_ACTIVE,
            'order'=>'priority DESC',
        ),
        'pagination'=>array(
            'pageSize'=>yii::app()->params['pageSize'],
        ),
    ));
        return $dataProvider;
    }
    public static function getNewsModelByNewsType($newsType)
    {
        $status = News::STATUS_ACTIVE;
        $model = News::model()->findAll("newstype_id = $newsType and status = $status order by priority DESC");
        return $model;
    }
    /**
     * Get the news content
     * format the content before return
     * @author : nonglei
     * @date   : 12/05/2014
     */
    public function getFormatsContent()
    {
        $content = strip_tags($this->content);

        if (mb_strlen($content, 'utf-8') > 200)
        {
            return mb_substr($content, 0, 150, 'utf-8') . "...<br/>" .CHtml::link('<b>Read more</b>', array('view', 'id'=>$data->news_id));
        }
        else
        {
            return $content;
        }


        return $text;
    }
    public function getType()
    {
        return $this->newstype_id;
    }
    /**
     * return the title of newstype 
     * @author : nonglei
     * @date   : 15/03/2014
     */
    public function getStatusOptions()
    {
        return array (
            self::STATUS_ACTIVE => 'ACITVE',
            self::STATUS_DEACTIVE => 'DEACTIVE',
            );
    }
    public function getNewsTypeOptions()
    {
        $model = Newstype::model()->findAll("status = 1");
        $data = array();
        foreach($model as $elem)
        {
            $data[$elem->newstype_id] = $elem->title;
        }
        return $data;
    }
    public function isImageContentNews()
    {
        return $this->newstype_id == News::TYPE_IMAGECONTENT;
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, newstype_id, title, content, status', 'required'),
			array('user_id, newstype_id, priority, views, status', 'numerical', 'integerOnly'=>true),
			array('title, url', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('news_id, user_id, newstype_id, priority, views, published_date, title, content, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
            'newstype'=>array(self::BELONGS_TO,'Newstype','newstype_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'news_id' => Yii::t('models/News','NewsID'),
			'user_id' => Yii::t('models/News','UserID'),
			'newstype_id' => Yii::t('models/News','News Type'),
			'priority' => Yii::t('models/News','Priority'),
			'views' => Yii::t('models/News','Views'),
			'published_date' =>Yii::t('models/News','Published Date'),
			'title' => Yii::t('models/News','Title'),
            'content' => Yii::t('models/News','Content'),
            'url'=>Yii::t('models/News','ImageURL'),
			'status' => Yii::t('models/News','Status'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

	//	$criteria->compare('news_id',$this->news_id);
		$criteria->compare('user_id',$this->user_id);
        $criteria->compare('newstype_id',$this->newstype_id);
	//	$criteria->compare('priority',$this->priority);
		$criteria->compare('views',$this->views);
		$criteria->compare('published_date',$this->published_date,true);
		$criteria->compare('title',$this->title,true);
	//  $criteria->compare('content',$this->content,true);
		$criteria->compare('status',$this->status);
    //  $criteria->compare('url',$this->url,true);
		$criteria->order = 'news_id DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
